package com.develop.mybluetooth.activity

import android.content.ClipData
import android.content.ClipboardManager
import android.content.Intent
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.getSystemService
import cat.ereza.customactivityoncrash.CustomActivityOnCrash
import com.develop.mybluetooth.MainActivity
import com.develop.mybluetooth.databinding.ActivityErrorBinding


class ErrorActivity : AppCompatActivity() {

    var mBind : ActivityErrorBinding?= null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mBind = ActivityErrorBinding.inflate(layoutInflater)
        setContentView(mBind?.root)
        val config = CustomActivityOnCrash.getConfigFromIntent(intent)
//        mBind?.ivErrorBack?.setOnClickListener{
//            config?.run {
//                CustomActivityOnCrash.restartApplication(this@ErrorActivity, this)
//            }
//        }
        mBind?.errorRestart?.setOnClickListener{
            config?.run {
                CustomActivityOnCrash.restartApplication(this@ErrorActivity, this)
            }
        }
        mBind?.errorSendError?.setOnClickListener {
            CustomActivityOnCrash.getStackTraceFromIntent(intent)?.let {
                AlertDialog.Builder(this)
                    .setTitle("反馈问题")
                    .setMessage("复制成功之后，可分享或粘贴到微信或其他通讯软件")
                    .setPositiveButton("复制") { _, _ ->
                        val mClipData = ClipData.newPlainText("errorLog",it)
                        // 将ClipData内容放到系统剪贴板里。
                        getSystemService<ClipboardManager>()?.setPrimaryClip(mClipData)
                        Toast.makeText(this, "复制成功", Toast.LENGTH_SHORT).show()
                    }
                    .setNegativeButton("重启") { _, _ ->
                        startActivity(Intent(this,MainActivity::class.java))
                     }
                    .show()
            }
        }
    }
}